<?php

include_once "function_xml_basic.php";

$root_path_dhcp = "/MINI/NETWORK/DHCP";
$root_path_dhcp_server = $root_path_dhcp. "/SERVER";
$root_path_dhcp_addrpool = $root_path_dhcp_server. "/ADDRPOOLS";
$root_path_dhcp_macbind = $root_path_dhcp_server. "/MACBIND";

$root_path_dhcp_client= $root_path_dhcp. "/CLIENT";
$root_path_dhcp_relay = $root_path_dhcp. "/RELAY";

function GetXmlDhcpServerEnabled()
{
	global $root_path_dhcp_server;
	$query_string = $root_path_dhcp_server;
	$key_array = array('Enabled', 'Dev');
	$list = GetAttributList($query_string, $key_array);
	$arr =  $list[0];
	
	return $arr;
}
//print GetXmlDhcpServerEnabled();

function SetXmlDhcpServerGlobalSetting($in_array)
{
	//DisableAllDhcServer();
	global $root_path_dhcp_server;
	$query_string = $root_path_dhcp_server. "['1']";
	EditAllAttrOfNode($query_string, $in_array);
	ApplySysDhcp();
}

function GetXmlDhcpServerAddrPoolList()
{
	global $root_path_dhcp_addrpool;
	$query_string = $root_path_dhcp_addrpool. "/CONFIG";
	$key_array = array('Domain', 'Subnet', 'Mask', 'AddrRangStart', 'AddrRangEnd', 
			   'DefaultTime', 'MaxTime', 'GateWay', 'DNS1', 'DNS2', 'Type');
	return GetAttributList($query_string, $key_array);
}
/*$list = GetXmlDhcpServerAddrPollList();
foreach ($list as $l)
{
	print $l['AddrRangStart']. "\n";
	print $l['Mask']. "\n";
}
*/

function GetXmlDhcpServerMacBindList()
{
	global $root_path_dhcp_macbind;
	$query_string = $root_path_dhcp_macbind. "/CONFIG";
	$key_array = array('Name', 'Mac', 'Addr', 'Type');
	return GetAttributList($query_string, $key_array);
}
/*
$list = GetXmlDhcpServerMacBindList();
foreach ($list as $l)
{
	print $l['Mac']. "\n";
	print $l['Addr']. "\n";
}*/

function GetSysDhcpDistributeList()
{
	return ;
}

function DelXmlDhcpSpecialAddrPool($number)
{
	global $root_path_dhcp_addrpool;

	$query_string = $root_path_dhcp_addrpool. "/CONFIG[". $number. "]";
	DelSpecialNode($query_string);
}
function AppendXmlDhcpAddrPool($in_array)
{
	global $root_path_dhcp_addrpool;
        $node_name = "CONFIG";
	$query_string = $root_path_dhcp_addrpool;
	$in_array['Type'] = "1";
	AppendAllAttrOfNode($query_string, $node_name, $in_array);
}
function DelXmlDhcpSpecialMacBind($name)
{
	global $root_path_dhcp_macbind;

	$query_string = $root_path_dhcp_macbind. "/CONFIG[@Name='". $name. "']";
	DelSpecialNode($query_string);
}

function AppendXmlDhcpMacBind($in_array)
{
	global $root_path_dhcp_macbind;
        $node_name = "CONFIG";
	$query_string = $root_path_dhcp_macbind;
	$in_array['Type'] = "1";
	AppendAllAttrOfNode($query_string, $node_name, $in_array);
}

function SetXmlDhcpClient($in_array)
{
	//DisableAllDhcServer();
	global $root_path_dhcp_client;
	$query_string = $root_path_dhcp_client. "['1']";
	EditAllAttrOfNode($query_string, $in_array);
	ApplySysDhcp();
}

function SetXmlDhcpRelay($in_array)
{
	//DisableAllDhcServer();
	global $root_path_dhcp_relay;
	$query_string = $root_path_dhcp_relay. "['1']";
	EditAllAttrOfNode($query_string, $in_array);
	ApplySysDhcp();
}

function DisableAllDhcServer()
{
	$in_array['Enabled'] = "0";
	SetXmlDhcpServerGlobalSetting($in_array);
	SetXmlDhcpClient($in_array);
	SetXmlDhcpRelay($in_array);
}

function GetXmlDhcpClient()
{
	global $root_path_dhcp_client;
	$query_string = $root_path_dhcp_client;
	$key_array = array('Enabled', 'Interface');
	$list = GetAttributList($query_string, $key_array);
	$array =  $list[0];
	return $array;
}

function GetXmlDhcpRelay()
{
	global $root_path_dhcp_relay;
	$query_string = $root_path_dhcp_relay;
	$key_array = array('Enabled', 'Interface','ServerAddr');
	$list = GetAttributList($query_string, $key_array);
	$array =  $list[0];
	return $array;
}
function StopSysAllDhcpService()
{
	// if [ -e  /var/run/dhcpd.pid ]; then kill -9 ` cat /var/run/dhcpd.pid;`; fi
	$kill_dhcpd = "if [ -e /var/run/dhcpd.pid ]; then kill -9 `cat /var/run/dhcpd.pid ` 1>/dev/null 2>&1; fi; ";
	$kill_dhclient = "if [ -e /var/run/dhclient.pid ]; then kill -9 `cat /var/run/dhclient.pid ` 1>/dev/null 2>&1; fi;";
	$kill_dhcrelay = "if [ -e /var/run/dhcrelay.pid ]; then kill -9 `cat /var/run/dhcrelay.pid ` 1>/dev/null 2>&1; fi;";
	$rm_pid = "rm -rf /var/run/dhcpd.pid /var/run/dhclient.pid /var/run/dhcrelay.pid 1>/dev/null 2>&1;";

	$command = $kill_dhcpd. $kill_dhclient. $kill_dhcrelay. $rm_pid ;
	//print $command;
	$ret = shell_exec($command);
	return $ret;
}

function StartSysDhcpdService()
{
	$list = GetXmlDhcpServerEnabled();
	
	$service = $list['Enabled'];
	if ($service == 1)
	{
		$dev = $list['Dev'];
		ApplySysDhcpServerConfig();
		$start = "/usr/sbin/dhcpd ". $dev;
		//print $start;
		$ret = shell_exec($start);
		return $ret;
	}
	return ;
}
function StartSysDhclientService()
{	
	$client = GetXmlDhcpClient();
	if ($client["Enabled"] == 1)
	{
		$ilist = explode ("|",$client["Interface"]);
		$interface_op = "";
		foreach ($ilist as $i)
		{
			if ($i == NULL)
				continue;
			$interface_op .= " ". $i. " ";
		}
		$start = "/usr/sbin/dhclient ". $interface_op;
		$ret = shell_exec($start);
		return $ret;
	}
	return ;
}
function StartSysDhcrelayService()
{
	$relay = GetXmlDhcpRelay();
	if ($relay["Enabled"] == 1)
	{
		$ilist = explode ("|",$relay["Interface"]);
		$interface_op = "";
		foreach ($ilist as $i)
		{
			if ($i == NULL)
				continue;
			$interface_op .= " -i ". $i. " ";
		}
		$start = "/usr/sbin/dhcrelay ". $interface_op. $relay["ServerAddr"];
		//print $start;
		$ret = shell_exec($start);
		return $ret;
	}
	return ;
}

function ApplySysForSpecialHostAddr()
{
	$command_addr = "";
	$list = GetXmlDhcpServerMacBindList();
	foreach ( $list as $l )
	{ // 'Name', 'Mac', 'Addr'
		$name = $l['Name'];
		$mac = $l['Mac'];
		$addr = $l['Addr'];
		
		if ($name == NULL || $mac == NULL || $addr == NULL)
			continue;

		$command_addr .= "host ". $name. " {\n hardware ethernet ". $mac. 
				 " ; fixed-address ". $addr. " ;\n }\n";
	}
	return $command_addr;
}

function ApplySysDhcpServerConfig()
{
	include_once "function_misc_net.php";
	$config_file = "/etc/dhcpd.conf";

	$glob = "ddns-update-style interim; \n ignore client-updates; \n";
	
	$command_subnet = "";
	$list = GetXmlDhcpServerAddrPoolList();
	
	foreach ($list as $l )
	{ /* 'Domain', 'Subnet', 'Mask', 'AddrRangStart', 'AddrRangEnd',
             'DefaultTime', 'MaxTime', 'GateWay', 'DNS1', 'DNS2', 'Type' */
		$subnet = $l['Subnet'];
		$mask = $l['Mask'];
		if ($subnet == NULL || $mask == NULL)
		{
			continue;
		}
		$broadcast = ipv4broadcast($subnet, $mask); 
		$command_subnet .= "subnet ". $subnet. " netmask ". $mask. " { \n";

		if ($l['GateWay'] != '')
			$command_subnet .= " option routers ". $l['GateWay']. " ;\n";

		$command_subnet .= " option subnet-mask ". $mask. " ;\n";

		if ($broadcast != '')
			$command_subnet .= " option broadcast-address ". $broadcast. " ;\n";

		if ($l['DNS1'] != '')
			$command_subnet .= " option domain-name-servers ". $l['DNS1']. " ;\n";
		if ($l['DNS2'] != '')
			$command_subnet .= " option domain-name-servers ". $l['DNS2']. " ;\n";

		$command_subnet .= " range dynamic-bootp ". $l["AddrRangStart"]." ". $l['AddrRangEnd']. " ;\n";

		if ($l['DefaultTime'] != '')
			$command_subnet .= " default-lease-time ". $l["DefaultTime"]. " ;\n";

		if ($l['MaxTime'] != '')
			$command_subnet .= " max-lease-time ". $l["MaxTime"]. " ;\n";
				   
		$command_subnet .= " }\n";
	}
	$addr = ApplySysForSpecialHostAddr();
	$command_string = $glob. $command_subnet. $addr;
	$command = "echo \" ". $command_string . "\" > ". $config_file;
	//print $command;
	$ret = shell_exec($command);
	return $ret;
}

function ApplySysDhcp()
{
	StopSysAllDhcpService();
	StartSysDhcpdService();
	StartSysDhclientService();
	StartSysDhcrelayService();
}
?>
